<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/3/8
  Time: 13:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加智能锁</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport"
	content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
	<link href="${pageContext.request.contextPath}/static/css/KeysManager/AddLock.css?date=01208777" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer_mobile/need/layer.css" rel="stylesheet">
    <script rel="script" src="${pageContext.request.contextPath}/static/js/KeysManager/index.js?sate=2"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer_mobile/layer.js" type="text/javascript"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.form.js" type="text/javascript"></script>
    <script src="${pageContext.request.contextPath}/static/js/yahlj-app.js"></script>
    <style type="text/css">
        input{
            box-sizing: content-box;
        }
      </style>
</head>
<body>
<div class="title clearfix">
	<div class="lf">
		<div>请打开蓝牙设备，自动获取信息</div>
		<div>也可扫一扫获取信息</div>
	</div>
	<div class="rt Scan_btn" onclick="scanclick()">
		扫一扫
	</div>
 <%--    <img class="scan" onclick="scanclick()" src="${pageContext.request.contextPath}/static/images/KeysManager/scan.png"/> --%>
   <!--  <p class="scan-info">扫码自动输入PID输入、激活码</p> -->
   
</div>
<form method="post">
<input type="hidden" name="type" id="type" value="${type }" />
    <div class="content">
        <div class="item">
        	<div class="item_title">PID</div>
          <%--   <img class="icon" src="${pageContext.request.contextPath}/static/images/KeysManager/PID.png"/> --%>
            <input type="text" name="pid" id="pid" class="edittext" placeholder="请输入PID"  value="${pid}" />
            <div class="gain_PID1" id='gain_PID' onclick="pid_change()">重新获取</div>
        </div>
        <div class="item">
        	<div class="item_title">激活码</div>
            <%-- <img class="icon" src="${pageContext.request.contextPath}/static/images/KeysManager/activationCode.png"/> --%>
            <input type="text" name="checkCode" id="checkCode" class="edittext" placeholder="请输入激活码"  value="${checkcode}" />
        </div>
        <div class="item">
        	<div class="item_title">锁的名称</div>
           <%--  <img class="icon" src="${pageContext.request.contextPath}/static/images/KeysManager/key.png"/> --%>
            <input type="text" name="installLockName" id="installLockName" class="edittext" placeholder="请输入智能锁名称（如：我的家门）"/>
        </div>
    </div>
</form>
    <div class="foot">
        <button name="enter" id="submit" onclick="doUpload()">确定</button>
    </div>

<script>

var appid='';
var loginedFlag='${loginedFlag}';//app的登录状态
var loginedFlag_='${loginedFlag}';

window.onload=function(){
	setTimeout("dosm()",500);	
}
function dosm(){
	//pid_change();
	
	hljJs.getAppid({
		success:function(res){
			appid = res;
		},
		fail:function(res){
			layerAlert("获取APP信息失败");
		}
	});	
}

function scanclick(){
	hljJs.scanCode({
		success:function(res){
			
		},
		fail:function(res){
			layer.open({
				title:'友情提示',
        	    content: res,
        	    btn: '我知道了'
        	  });
		}
	});
}
function callbackScanCode(result){

	if(result.code==1){
		var resultStr = result.resultStr;
		if(resultStr.indexOf("tlocks_a.action")>-1){
			window.location.href = resultStr;
			return;
		}
		if(resultStr.indexOf(",")>0){
			var arr = resultStr.split(",");
			$("#pid").val(arr[0]);
			$("#checkCode").val(arr[1]);
		}else{
			if(resultStr.indexOf("?p=")<0){
				layer.open({
					title:'友情提示',
		    	    content: "无法获取PID，请重试",
		    	    btn: '我知道了'
		    	  });
		    	  return;
			}
			if(resultStr.indexOf("&c=")<0){
				layer.open({
					title:'友情提示',
		    	    content: "无法获取激活码，请重试",
		    	    btn: '我知道了'
		    	  });
				 return;
			}
			var arr1 = resultStr.split("?p=");
			var arr2 = arr1[1].split("&c=");
			var arr3 = arr2[1].split("&v=");	
			$("#pid").val(arr2[0]);
			$("#checkCode").val(arr3[0]);
			
		}
	}else if(result.code==2){
	}else{
		layer.open({
			title:'友情提示',
    	    content: "获取二维码扫描结果失败，请重试",
    	    btn: '我知道了'
    	  });
	}
}
	function doUpload(){
		$("#submit").attr("disabled", "true");
		var pid = $("#pid").val();
		var type = $("#type").val();
		var checkCode = $("#checkCode").val();
		var installLockName = $("#installLockName").val();
	    $.post("${pageContext.request.contextPath}/admin/tlocks_add.action", {
	    	'pid':pid,
	    	'checkCode':checkCode,
	    	'type':type,
	    	'installLockName': installLockName
         }, function (result) {
             result = JSON.parse(result)
             if (result.code == 1) {
//             	 layer.alert(result.msg);
            	 window.location.href = "${pageContext.request.contextPath}/admin/tlocks_lockManagerApp.action";
             }else{
            	 $("#submit").removeAttr("disabled");
             	layer.open({
             		title:'友情提示',
            	    content: result.msg,
            	    btn: '我知道了'
            	  });
             }
         });
	}
	
	     
	 function pid_change(){
	    	$("#gain_PID").text("正在获取");
	    	var okPid='';
	    	hljJs.scanBlue({
	    		success:function(res){
					if(res.code=='1'){
						if(res.blue_state=='0'){
							layerAlert('请开启蓝牙');
						}else{
							var macs='';
							var names ='';
							for(var i in res.data){
								var d = res.data[i];
								var name = d.name;
								var rssi = d.rssi;
								var manufacturerData = d.manufacturerData;
								var mac = '';
								if(name.length>0){
									if(name.indexOf('MD_')>-1){//我们设备名以MD_开头
										manufacturerData = manufacturerData.replace(" ","");
										if(manufacturerData.length>=16){
											mac = manufacturerData.substr(4,12);
										}else if(typeof(d.mac)!="undefined"&&d.mac.length>0){ 
											mac = d.mac;
										}else{
											continue;
										}
										macs+=mac+",";
										names+=name+",";
									}
								}
							}		
							if(macs.length>0){
								$.ajax({   
								    url:'${pageContext.request.contextPath}/admin/tlocks_getActiveInfo.action',   
								    type:'post',   
								    data:'mac='+macs+'&device_name='+names+'&appid='+appid,   
								    async : true, //默认为true 异步   
								    dataType:'json',
								    error: function(XMLHttpRequest, textStatus, errorThrown) {
								    	layerAlert('请求失败！');
								    },  
								    success:function(data){   
								    	var okFlag =  false;
								    	if (data.code == 1) {
								    		for(var o in data.data){
								    			var d = data.data[o]
								    			 if(d.bizerr=='1'){
													var pid = d.pid;
													var checkcode=d.checkcode;
													$("#pid").val(pid);
													$("#checkCode").val(checkcode);
													$("#gain_PID").text("重新获取");
													layerAlert_PID();
													okFlag=true;
								            	 }
								    		}
							             }else{

							             }
								    	
								    	if(!okFlag){
								    		layerAlert('未找到设备');
								    	}
								    }
								});
								
							}else{
								layerAlert('未找到设备，请靠近设备或确认设备状态正确');
							}
							
						}
					}else{
						layerAlert('获取失败');
					}
					$("#gain_PID").text("重新获取");
				},
				fail:function(res){
					if("-1000"==res){
						layerAlert('此版本app不支持蓝牙获取设备信息');
					}else{
						layerAlert('获取失败.');						
					}
					$("#gain_PID").text("重新获取");
				}
			});
	    } 
	 
	 	/*核对PID提示框 */
	 	function layerAlert_PID(){
	 		layer.open({
	 			title: '温馨提示',
	       	    content: "请与设备上的PID进行核对，如不符合将会影响您的设备操作",
	       	    btn: '确定'
	       	});
	 	}
	     
		function layerAlert(msg){
			layer.open({
	      	    content: msg,
	      	    btn: '我知道了'
	      	});
		}
		
	
</script>
</body>
</html>
